
import 'package:flutter/material.dart';

class ThreeFingerSwipeUpDetector extends StatefulWidget {
  final Widget child;
  final VoidCallback onThreeFingerSwipeUp;

  const ThreeFingerSwipeUpDetector({
    Key? key,
    required this.child,
    required this.onThreeFingerSwipeUp,
  }) : super(key: key);

  @override
  _ThreeFingerSwipeUpDetectorState createState() =>
      _ThreeFingerSwipeUpDetectorState();
}

class _ThreeFingerSwipeUpDetectorState
    extends State<ThreeFingerSwipeUpDetector> {
  final Map<int, Offset> _pointerStartPositions = {};
  final Map<int, double> _pointerDeltaY = {};
  final double threshold = 10; // 滑动阈值（可调整）

  void _reset() {
    _pointerStartPositions.clear();
    _pointerDeltaY.clear();
  }

  @override
  Widget build(BuildContext context) {
    return Listener(
      onPointerDown: (event) {
        _pointerStartPositions[event.pointer] = event.position;

        if(_pointerStartPositions.length == 3) {
          widget.onThreeFingerSwipeUp.call();
        }
      },
      onPointerMove: (event) {
        if (_pointerStartPositions.containsKey(event.pointer)) {
          final start = _pointerStartPositions[event.pointer]!;
          final deltaY = event.position.dy - start.dy;
          _pointerDeltaY[event.pointer] = deltaY;
          if (_pointerStartPositions.length == 3 &&
              _pointerDeltaY.length == 3) {
            bool allUp = _pointerDeltaY.values
                .every((dy) => dy < -threshold); // 上滑：dy 是负的

            if (allUp) {
              widget.onThreeFingerSwipeUp();
              _reset();
            }
          }
        }
      },
      onPointerUp: (event) {
        _pointerStartPositions.remove(event.pointer);
        _pointerDeltaY.remove(event.pointer);
      },
      onPointerCancel: (event) {
        _pointerStartPositions.remove(event.pointer);
        _pointerDeltaY.remove(event.pointer);
      },
      child: widget.child,
    );
  }
}
